package handlers

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"swu.com/80p/internal/dbs"
)

// 删除行接口的 Gin Handler
func DeleteHandler(c *gin.Context) {
	tableName := c.Param("tableName")
	id := c.Param("id")
	primaryKeyName := getPrimayKey(c.Query("primaryKeyName"))

	result := dbs.GetDB().Table(tableName).Where(primaryKeyName+" = ?", id).Delete(GenericModel{})
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, getGinError(fmt.Errorf("database error: %v", result.Error)))
		return
	}

	c.JSON(http.StatusOK, getGinResult(gin.H{"deleted": result.RowsAffected}))
}
